import "./index.scss"
import {View, Text, Image, Button} from "@tarojs/components"
import {observer} from "mobx-react"
import {useContext, useEffect, useState} from "react"
import Taro, {useDidHide, useDidShow, useRouter} from "@tarojs/taro"
import {startMeasureApi} from "@/api/measureControl"

const MeasureControl = () => {
	let router = useRouter()

	const [devGId, setDevGId] = useState("")

	useEffect(() => {
		console.log(router)
		setDevGId(router.params.devGId)
	}, [])

	// 启动测量请求
	const startMeasureFunc = async () => {
		Taro.showLoading({
			title: "启动中",
			mask: true
		})
		let data = {
			devGId: devGId
		}
		await startMeasureApi(data)
			.then(res => {
				if (res.result == "success") {
					Taro.showToast({
						title: "启动成功",
						icon: "success",
						mask: true,
						duration: 1000
					})
				} else if (res.result == "error") {
					Taro.showToast({
						title: res.msg,
						icon: "none",
						mask: true,
						duration: 1000
					})
				}
			})
			.catch(err => {
				console.log("err", err)

				Taro.showToast({
					title: err.msg,
					icon: "none",
					mask: true,
					duration: 1000
				})
			})
			.finally(() => {
				setTimeout(() => {
					Taro.hideLoading()
				}, 3000)
			})
	}

	/* 数据  -------------------------------------------------------------------------- */

	return (
		<View className="measure_control_wrap">
			<View className="hanzi text-16 font-bold mt-14 mb-14 px-16">测量控制</View>
			<View className="hanzi text-14 text-xx_text mb-14 px-16">
				您可以对设备进行测量相关控制
				<View className="start_measure">
					<View className="title_box">
						<Text className="title_text">测量启动</Text>
					</View>
					<View className="btn_box">
						<View for="checkbox" class="switch" onClick={startMeasureFunc}>
							<Image
								className="start_btn_img"
								src="https://d.lohand.com:1112/images/ConstantBlueCloud/wxminiprogram/start1.svg"
							></Image>
							<Text className="btn_text">启动测量</Text>
						</View>
					</View>
				</View>
			</View>
		</View>
	)
}
export default observer(MeasureControl)
